﻿var StudyParameterVm = function(jsonOfObject) {
    var self = this;

    self.Id = ko.observable();
    self.Type = "StudyParameterVm";
    self.Name = ko.observable();
    self.SortOrderId = ko.observable(null);
    self.Parameter = ko.observable();
    self.InputConfigurations = ko.observableArray([]);
    self.AreThereInputConfigurations = ko.pureComputed(function () {
        return self.InputConfigurations().length > 0;
    });
    self.AreThereInputConfigurationChanges = ko.observable(false);

    self.ParameterId = ko.computed({
        read: function() {
            if (self.Parameter() == null)
                return null;
            return self.Parameter().Id();
        },
        deferEvaluation: true
    });

    self.ParameterInputTypeName = ko.computed(function() {
        if (self.Parameter() == null) return "";
        return self.Parameter().InputType().Name();
    });

    self.IsNew = ko.computed(function () {
        return self.Id() == null;
    });

    self.OnInputConfigurationChange = function () {
        var hasChanges = false;
        for (var i in self.InputConfigurations()) {
            if (self.InputConfigurations()[i].IsDirty()) {
                hasChanges = true;
                break;
            }
        }
        self.AreThereInputConfigurationChanges(hasChanges);
    };

    self.Refresh = function (jsonData) {
        var mapping = {
            'Parameter': {
                create: function(options) {
                    return new ParameterVm(options.data);
                }
            },
            'InputConfigurations': {
                create: function (options) {
                    switch(options.data.Feature) {
                        case "Maximum Length" :
                            return new ParameterInputMaximumLengthConfiguration(options.data);
                        case "Required":
                            return new ParameterInputRequiredConfiguration(options.data);
                        default:
                            return new BaseParameterInputConfiguration(options.data);
                    }
                }
            }
        };

        ko.mapping.fromJS(jsonData, mapping, self);
    };

    self.SaveConfigurations = function () {
        var configurationsToUpdate = [];

        self.InputConfigurations().map(function (inputConfiguration) {
            if (inputConfiguration.IsDirty()) {
                configurationsToUpdate.push({ Id: inputConfiguration.Id(), Value: inputConfiguration.Value() });
            }
        });

        $.post("/StudyParameterInputConfigurations/Save", { configurationsToUpdate: configurationsToUpdate })
            .done(function () {
                self.InputConfigurations().map(function (inputConfiguration) { inputConfiguration.OnUpdateComplete(); });
                self.AreThereInputConfigurationChanges(false);
            })
            .fail(function (msg) {
                window.alert(msg.responseText);
            });
    };

    self.Refresh(jsonOfObject);
    self.DirtyFlag = ko.dirtyFlag(self, false);

    self.InputConfigurations().map(function (inputConfiguration) {
        inputConfiguration.IsDirty.subscribe(self.OnInputConfigurationChange, this);
    });

    self.MapToJson = function () {
        var json = {};
        json["Id"] = self.Id();
        json["Name"] = self.Name();
        json["SortOrderId"] = self.SortOrderId();
        json["Parameter"] = self.Parameter().MapToJson();
        return json;
    };
};